#define DBC_CHECK_PRECONDITION

using System;
using System.Xml;

using DesignByContract;

using Outcoding.GraphGenerator.Entities;

namespace Outcoding.GraphGenerator.XmlBuilders {
    public class LabelXmlBuilder : IXmlBuilder {

        private Label mResultObject = null;

        public object ResultObject {
            get {
                return mResultObject;
            }
        }

        public LabelXmlBuilder(XmlNode labelXmlNode) {
            Check.Require(labelXmlNode != null, "Xml source node cannot be null");
            Check.Require(labelXmlNode.Name.CompareTo(XmlTagNames.label.ToString()) == 0, "A Label Xml node must be supplied.");
            this.BuildObject(labelXmlNode);
        }

        private void BuildObject(XmlNode labelXmlNode) {
            mResultObject = new Label();
            try {
                if (labelXmlNode.Attributes["font-name"] != null)
                    mResultObject.FontName = ParseFontNameAttribute(labelXmlNode);
                if (labelXmlNode.Attributes["font-size"] != null)
                    mResultObject.FontSize = ParseFontSizeAttribute(labelXmlNode);
                if (labelXmlNode.Attributes["font-decoration"] != null) {
                    mResultObject.IsFontBold = ParseIsFontBoldAttribute(labelXmlNode);
                    mResultObject.IsFontItalic = ParseIsFontItalicAttribute(labelXmlNode);
                    mResultObject.IsFontUnderline = ParseIsFontUnderlineAttribute(labelXmlNode);
                }
                if (labelXmlNode.Attributes["visible"] != null)
                    mResultObject.IsVisible = ParseIsVisibleAttribute(labelXmlNode);
                mResultObject.Text = ParseTextValue(labelXmlNode);

            } catch (DesignByContract.PreconditionException preExc) {
                //log error
                mResultObject = null;
                throw;
            }
        }

        private string ParseFontNameAttribute(XmlNode labelXmlNode) {
            Check.Require(labelXmlNode.Attributes["font-name"] != null, "font-name attribute not present");
            return labelXmlNode.Attributes["font-name"].Value;
        }

        private int ParseFontSizeAttribute(XmlNode labelXmlNode) {
            Check.Require(labelXmlNode.Attributes["font-size"] != null, "font-size attribute not present");
            return Int32.Parse(labelXmlNode.Attributes["font-size"].Value);
        }

        private bool ParseIsFontBoldAttribute(XmlNode labelXmlNode) {
            Check.Require(labelXmlNode.Attributes["font-decoration"] != null, "font-decoration attribute not present");
            return labelXmlNode.Attributes["font-decoration"].Value.Contains("bold");
        }

        private bool ParseIsFontItalicAttribute(XmlNode labelXmlNode) {
            Check.Require(labelXmlNode.Attributes["font-decoration"] != null, "font-decoration attribute not present");
            return labelXmlNode.Attributes["font-decoration"].Value.Contains("italic");
        }

        private bool ParseIsFontUnderlineAttribute(XmlNode labelXmlNode) {
            Check.Require(labelXmlNode.Attributes["font-decoration"] != null, "font-decoration attribute not present");
            return labelXmlNode.Attributes["font-decoration"].Value.Contains("underline");
        }

        private bool ParseIsVisibleAttribute(XmlNode labelXmlNode) {
            Check.Require(labelXmlNode.Attributes["visible"] != null, "visible attribute not present");
            return Boolean.Parse(labelXmlNode.Attributes["visible"].Value);
        }

        private string ParseTextValue(XmlNode labelXmlNode) {
            return labelXmlNode.InnerText.Trim();
        }
    }
}
